$(document).ready(function(){
	if($('body').find('.lazy').length){
			$("img.lazy").lazyload({
		 	effect : "fadeIn",
			 failure_limit : 4,
			 skip_invisible : true
		});
	}
	
	if($('body').find('.lazy2').length){
		
		$("img.lazy2").lazyload({
		 	effect : "fadeIn",
			 failure_limit : 4,
			 skip_invisible : true
		});
	}
	
	
		if($('body').find('.tooltip-hover').length){
			$('.tooltip-hover').each(function(){
				//$(this).next().find('.lazy').();
				
				$(this).tooltipster({
					contentAsHTML: true,
					content: $(this).next().html(),
					position: 'right',
					//interactive: true,
					speed: 200,
					functionReady: function(){
						newContent = $('<div>' + this.tooltipster('content') + '</div>');
						dataOriginal = newContent.find('.tooltip-content-image').attr('data-original');
						newContent.find('.tooltip-content-image').attr('src', dataOriginal);
						//newContent.find('.tooltip-content-image').lazyload();
						$(this).tooltipster('content', newContent.html());
					}
				});
				
				
				
				
			});
		}
		
		$('.tooltip-hover').on('mouseenter', function(){
			//alert('here');
			console.log($('.tooltipster-default').find('img').length);
		});
		
		
		$.scrollUp({
		    scrollName: 'toTop', // Element ID
		    scrollDistance: 100, // Distance from top before showing element (px)
		    topSpeed: 100, // Speed back to top (ms)
		    animation: 'fade', // Fade, slide, none
		    animationInSpeed: 200, // Animation in speed (ms)
		    animationOutSpeed: 200, // Animation out speed (ms)
		    scrollText: '<img src="public/images/top.png" alt="Về đầu trang" />', // Text for element
		    activeOverlay: false, // Set CSS color to display scrollUp active point, e.g '#00FFFF'
		  });
		
		// Search auto complete home page manga ---------
		if( $('#menu-search-box').length ) {
				$('#menu-search-box').select2({ 
					width: 300,
					minimumInputLength: 1,
						ajax: { 
						 quietMillis: 1000,
						url: "index.php?module=ajax&act=manga&opt=search&manga_search_home", 
						dataType: 'json', 
								data: function (term, page){ 
									return {q: term }; 
									}, 
									results: function (data, page) {
										
										return { results: data }; 
										
									}
						}
				
				}); 
				  $("#menu-search-box").on("change", function(e) {   
								//Feed chuong
							location.href=e.val;	
							
			   
				})
		}
		// End Search auto complete home page ---------
		
	$('.comment-info').on('click', function(e){
		 $('html, body').animate({
		        scrollTop: $("#right-fb-comment").offset().top
		    }, 300);
		
	});
	//Slider click
	$('.slide-btn').on('click', function(e){
		
			e.preventDefault();
			alias = $(this).attr('data-rel');
			dataTotal = $('#' + alias + '-slider-total-page').val();
			
			dataCurrentPage = $('#' + alias + '-slider-current-page').val();
			if($(this).attr('id') == (alias + '-btn-slider-prev')){
				dataGetPage = parseInt(dataCurrentPage) - 1;
				if(dataGetPage <= 0) dataGetPage = dataTotal;
			}else{
				dataGetPage = parseInt(dataCurrentPage) + 1;
			}
			
			ajaxLoadSlider(alias, dataGetPage, dataTotal);
			
		});
	//End slider click
	
});
		function modalBox(id, postType, popupControl){
			
			if(!$('#popupElem-innerItem-' + id + '-' + postType).text().length){
				$('#innerItem-'+id+'-'+postType).append('<div role="dialog" id="popupElem-innerItem-' + id + '-' + postType + '" class="modal" style="display: none;">');
				$('#popupElem-innerItem-' + id + '-' + postType).append('<div class="modal-dialog modal-wide" id="content-innerItem-'+ id +'">');
				$('#popupElem-innerItem-' + id + '-' + postType).append('</div><!-- end .modal-dialog --></div><!-- end .modal .fade -->');
				$('#popupElem-innerItem-'+id+'-'+postType+ ' > div.modal-dialog').append('<div id="loadMoreItems" class="normalLoadBtn" style="margin-top:20px;"><img  width="20" src="public/images/loading.gif" alt="loading" /> Loading </div><!-- end #loadMoreItems -->');
				$('#popupElem-innerItem-' + id + '-' + postType).modal();
				ajaxCallRequest(id, postType, popupControl);
			}else{
				$('#popupElem-innerItem-' + id + '-' + postType).modal();
				$('#popupControl-innerItem-'+id+'-'+postType).show();
				//FB.XFBML.parse(document.getElementById('popupControl-innerItem-'+ id+'-'+postType));
			}
		}
		
		function popupTinymce(){
			
		}
		
		function loadLoadingImg(img,width,height){
			if(img.indexOf('http') > -1){
				return '<img width="'+width+'" height="'+height+'" src="'+img+'" alt="Loading" />';
			}else{
				return '<img width="'+width+'" height="'+height+'" src="'+baseUrl+'/public/images/'+img+'" alt="" />';
			}
		}
		
		function replacePopupMessage(popupId, message){
			if($('#'+popupId).find('.alert').length){
				$('#'+popupId).find('.alert').fadeOut('slow').remove();
				$('#'+popupId).find('.modal-body').html(message);
				$('#'+popupId).find('.alert').fadeIn('slow');
				
			}else{
				$('#'+popupId).find('.modal-body').append(message);
				$('#'+popupId).find('.alert').css('display', 'none').fadeIn().delay(2000).fadeOut();
				var a = setTimeout(function(){
					$('#'+popupId).find('.alert').remove();
					//alert('here');
				},3000);
			}
			
			
		}
		function popupFuncMessage(message, type){
			var newMessage = '';
			$.each(message, function(e, index){
				if(typeof index != 'undefined'){
					newMessage += index + '<br />';
				}
			});
			return '<div class="alert '+ type +'">'+newMessage+'</div>';
		}
		
		function popupFuncButtons(btnsArray){
			//name,id,onclick
			btns = '';
			$.each(btnsArray, function(e, index){
				if(typeof index != 'undefined'){
					btns += '<button type="button" class="btn '+index.btnClass+'" id="'+index.btnId+'" onclick="'+index.btnOnClick+'">'+index.btnName+'</button>';
				}
			});
			return btns;
		}
		
		//Pop up with function
		//String title, String message, String btns
		function popupFunc(id, title, message, btns, modalClass){
			var messages;
			modalClass = "modal-dialog " + modalClass;
			$('.popup_func_message').remove();
			if(modalClass.indexOf('modal-btns-both') > 0){
				btnsTop = $('<div><button type="button" class="btn btn-default" data-dismiss="modal">Hủy</button>'+btns+'</div>');
				btnsTop.find('button').addClass('pull-right').css('margin', '0px 2px');
				btnsTop = btnsTop.html();
			}else{
				btnsTop = '';
			}
			
			$modal = '<div class="modal fade popup_func_message" id="'+id+'" style="display:none"> \
				  <div class="'+modalClass+'">\
			    <div class="modal-content">\
			      <div class="modal-header">\
			      <button type="button" class="close pull-right" data-dismiss="modal" aria-hidden="true">&times;</button>\
				  	'+btnsTop+'\
			        <h4 class="modal-title">'+title+'</h4>\
			      </div>\
			      <div class="modal-body">'+message+'</div>\
			      <div class="modal-footer">'+btns+'<button type="button" class="btn btn-default" data-dismiss="modal">Hủy</button>\</div>\
			    </div><!-- /.modal-content -->\
			  </div><!-- /.modal-dialog -->\
			</div><!-- /.modal -->';
			$('body').append($modal);
			$('.popup_func_message').modal();
		}
			//<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>\
			
			
			
		
		
		function popupMessage(title, message, type){
			var newMessage = '';
			var messages;
		
			$.each(message, function(e, index){
				if(typeof index != 'undefined'){
					newMessage += index + '<br />';
				}
			});
			
			if(!$('#popup_message').length){
				
				$modal = '<div class="modal fade" id="popup_message" style="display:none"> \
					  <div class="modal-dialog">\
				    <div class="modal-content">\
				      <div class="modal-header">\
				        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>\
				        <h4 class="modal-title">'+title+'</h4>\
				      </div>\
				      <div class="modal-body">\
						<div class="alert '+ type +'">'+newMessage+'</div>\
				      </div>\
				      <div class="modal-footer">\
				        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>\
				      </div>\
				    </div><!-- /.modal-content -->\
				  </div><!-- /.modal-dialog -->\
				</div><!-- /.modal -->';
				$('body').append($modal);
			}else{
			
				$('#popup_message').find('.modal-header').find('h4').html(title);
				$('#popup_message').find('.modal-body').find('div.alert').html(newMessage);
				$('#popup_message').find('.modal-body').find('div.alert').removeClass().addClass('alert ' + type);
			}
			
			$('#popup_message').modal();
		}
		
		// Slider function -------
		function ajaxLoadSlider(alias, page, totalPage){
			/*
			 *  alias: decu|moinhat
			 *  page: page muá»'n láº¥y dá»¯ liá»‡u
			 *  totalPage: tá»•ng táº¥t cáº£ cÃ¡c page
			 */
			if(page <= 0 || page > totalPage){
				page = 1;
			}
			
			$('.'+alias+'-content').css('opacity', '0.3');
			
			$('#wrap-' + alias + '-loading')
			.css('width', $(('#wrap-' + alias + '-loading')).parent().width())
			.css('height', $(('#wrap-' + alias + '-loading')).parent().height())
			.css('opacity', '1').css('position', 'absolute')
			.css('z-index','999').css('display', 'block');$('.loading').center();
			var contentWrapper = '#' + alias + '-wrapper';
	
			var pageBlockId = alias + '-content-slide-' + page;
			
			if($('#'+pageBlockId).html()){
				$('.' + alias + '-content').hide();
				$('#wrap-' + alias + '-loading').css('display', 'none');
				$('.' + alias + '-content').css('opacity', '1');
				$('#' + pageBlockId).fadeIn();
				$('#' + alias + '-slider-current-page').val(page);
				$('#' + alias + '-show-current-page').html(page);
			}else{
				
				if(alias.indexOf('video')!=-1){
					var act='video';
				}
				if(alias.indexOf('truyen')!=-1){
					var act='truyen';
				}
				else{
					var act='manga';
				}
				
				
				$.ajax({
					url: 'index.php?module=ajax&act='+act+'&opt=slider',
					type: 'POST',
					cache: false,
					data:{alias: alias, page: page},
					success: function(returnSlider){
						
						if(returnSlider) newData = $.parseJSON(returnSlider);
						if(newData.error){
							
						}else{
							$(contentWrapper).append('<div style="display:none;" id="' + pageBlockId + '" class="'+alias+'-content">'+newData.content+'</div>');
							$('.' + alias + '-content').hide();
							$('#' + pageBlockId).fadeIn();
							$('#' + alias + '-slider-current-page').val(page);
							$('#' + alias + '-show-current-page').html(page);
						}
					},
					
					complete: function(){
						$('#wrap-' + alias + '-loading').css('display', 'none');
						$('.' + alias + '-content').css('opacity', '1');
						$('.lazy').lazyload();
						
					}
				});
			}
			
			
			
		}
		// End Slider function -------
		function strpos(haystack, needle, offset) {
		   var i = (haystack + '')
			.indexOf(needle, (offset || 0));
		  return i === -1 ? false : i;
		}
		function explode(delimiter, string, limit) {
  //  discuss at: http://phpjs.org/functions/explode/
  // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  //   example 1: explode(' ', 'Kevin van Zonneveld');
  //   returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
			  if (arguments.length < 2 || typeof delimiter === 'undefined' || typeof string === 'undefined') return null;
			  if (delimiter === '' || delimiter === false || delimiter === null) return false;
			  if (typeof delimiter === 'function' || typeof delimiter === 'object' || typeof string === 'function' || typeof string ===
				'object') {
				return {
				  0: ''
				};
			  }
			  if (delimiter === true) delimiter = '1';
			
			  // Here we go...
			  delimiter += '';
			  string += '';
			
			  var s = string.split(delimiter);
			
			  if (typeof limit === 'undefined') return s;
			
			  // Support for limit
			  if (limit === 0) limit = 1;
			
			  // Positive limit
			  if (limit > 0) {
				if (limit >= s.length) return s;
				return s.slice(0, limit - 1)
				  .concat([s.slice(limit - 1)
					.join(delimiter)
				  ]);
			  }
			
			  // Negative limit
			  if (-limit >= s.length) return [];
			
			  s.splice(s.length + limit);
			  return s;
		}
		
		function loadFansubEditHtml(data){
			data.name = data.name != null ? data.name : '';
			data.facebook = data.facebook != null ? data.facebook : '';
			data.dangky = data.dangky != null ? data.dangky : '';
			data.hinhanh = data.hinhanh != null ? data.hinhanh : '';
			data.gioithieu = data.gioithieu != null ? data.gioithieu : '';
			strCmt = 
				'<div id="wrap-fansub-popup-form">\
					<label for="popup-fansub-name">Tên fansub</label>\
					<input placeholder="Tên fansub" style="margin-bottom: 5px;" type="text" class="form-control blog-title-input"\
				value="'+data.name+'" id="popup-fansub-name" name="popup-fansub-name" />\
				<label for="popup-fansub-facebook">Facebook (link)</label>\
				<input placeholder="Link facebook fansub" style="margin-bottom: 5px;" type="text" class="form-control blog-title-input"\
					value="'+data.facebook+'" id="popup-fansub-facebook" name="popup-fansub-facebook" />\
				<label for="popup-fansub-reg">Đăng kí(link thành viên đăng kí tham gia fansub)</label>\
				<input placeholder="Link đăng kí thành viên fansub" style="margin-bottom: 5px;" type="text" class="form-control blog-title-input"\
					value="'+data.dangky+'" id="popup-fansub-reg" name="popup-fansub-reg" />\
				<label for="popup-fansub-image">Hình đại diện (link)</label>\
				<input placeholder="Link hình đại diện fansub" style="margin-bottom: 5px;" type="text" class="form-control blog-title-input"\
					value="'+data.hinhanh+'" id="popup-fansub-image" name="popup-fansub-image" />\
				<label for="popup-fansub-intro">Giới thiệu</label>\
				<textarea placeholder="Giới thiệu fansub" rows="5"\
					class="blog-content-textarea form-control"\
					id="popup-fansub-intro" name="popup-fansub-intro">'+data.gioithieu+'</textarea>\
				<div style="display: none">\
					<input type="text" class="form-control blog-title-input"\
						placeholder="Tiêu đề" id="blog_comment_conten" />\
				</div><Br />\
				</div>';
				return strCmt;
		}